package org.abc.fund.controller.dataSource;
import lombok.RequiredArgsConstructor;
import org.abc.fund.service.dataSourceCollection.FundAnnouncementCrawler;
import org.abc.fund.service.dataSourceCollection.FundAnnouncementService;
import org.abc.fund.service.dataSourceCollection.FundHoldingBatchService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/fund/ttjj/fetch")
@RequiredArgsConstructor
public class TTJJCollectionController {

    private final FundAnnouncementService fundAnnouncementService;
    private final FundHoldingBatchService batchService;
    private final FundAnnouncementCrawler fundAnnouncementCrawler;

    @PostMapping("/announcement/all")
    public String fetchAll() {
        fundAnnouncementService.fetchAllAnnouncements();
        return "获取全部基金公告任务已执行！";
    }

    @PostMapping("/holding/all")
    public String crawl(@RequestParam int year) {
        batchService.fetchAllFundHoldings(year);
        return "基金持仓采集完成（year=" + year + "）";
    }

    @GetMapping("/announcement/test")
    public String testAnnouncementContent(@RequestParam String fundCode,
                                          @RequestParam String annId) {
        String content = fundAnnouncementCrawler.testSingleAnnouncement(fundCode, annId);
        return "公告内容长度: " + (content != null ? content.length() : 0) +
                "\n内容预览: " + (content != null ? content.substring(0, Math.min(200, content.length())) : "null");
    }
}
